//+-----------------------------------------------------------------------------
//| Included files
//+-----------------------------------------------------------------------------
#include "Random.h"


//+-----------------------------------------------------------------------------
//| Global objects
//+-----------------------------------------------------------------------------
RANDOM Random;


//+-----------------------------------------------------------------------------
//| Constructor
//+-----------------------------------------------------------------------------
RANDOM::RANDOM()
{
	SetSeed();
}


//+-----------------------------------------------------------------------------
//| Destructor
//+-----------------------------------------------------------------------------
RANDOM::~RANDOM()
{
	//Empty
}


//+-----------------------------------------------------------------------------
//| Sets a new random seed
//+-----------------------------------------------------------------------------
VOID RANDOM::SetSeed()
{
	Seed = static_cast<UINT>(std::time(NULL));
	std::srand(Seed);
}


//+-----------------------------------------------------------------------------
//| Sets a new random seed
//+-----------------------------------------------------------------------------
VOID RANDOM::SetSeed(UINT NewSeed)
{
	Seed = NewSeed;
	std::srand(Seed);
}


//+-----------------------------------------------------------------------------
//| Returns the current random seed
//+-----------------------------------------------------------------------------
UINT RANDOM::GetSeed() CONST
{
	return Seed;
}


//+-----------------------------------------------------------------------------
//| Generates a random boolean
//+-----------------------------------------------------------------------------
BOOL RANDOM::Bool() CONST
{
	return (std::rand() % 2) == 0;
}


//+-----------------------------------------------------------------------------
//| Generates a random integer
//+-----------------------------------------------------------------------------
INT RANDOM::Int(INT Min, INT Max) CONST
{
	if(Max <= Min) return Min;

	return Min + std::rand() % (Max - Min + 1);
}


//+-----------------------------------------------------------------------------
//| Generates a random floating point number
//+-----------------------------------------------------------------------------
FLOAT RANDOM::Float(FLOAT Min, FLOAT Max) CONST
{
	if(Max <= Min) return Min;

	return Min + (Max - Min) * (static_cast<FLOAT>(std::rand()) / static_cast<FLOAT>(RAND_MAX + 1));
}


//+-----------------------------------------------------------------------------
//| Generates a random double precision floating point number
//+-----------------------------------------------------------------------------
DOUBLE RANDOM::Double(DOUBLE Min, DOUBLE Max) CONST
{
	if(Max <= Min) return Min;

	return Min + (Max - Min) * (static_cast<DOUBLE>(std::rand()) / static_cast<DOUBLE>(RAND_MAX + 1));
}


//+-----------------------------------------------------------------------------
//| Generates a random digit
//+-----------------------------------------------------------------------------
CHAR RANDOM::Digit() CONST
{
	return static_cast<CHAR>(Int('0', '9'));
}


//+-----------------------------------------------------------------------------
//| Generates a random letter
//+-----------------------------------------------------------------------------
CHAR RANDOM::Letter() CONST
{
	return Bool() ? UpperCaseLetter() : LowerCaseLetter();
}


//+-----------------------------------------------------------------------------
//| Generates a random lowercase letter
//+-----------------------------------------------------------------------------
CHAR RANDOM::LowerCaseLetter() CONST
{
	return static_cast<CHAR>(Int('a', 'z'));
}


//+-----------------------------------------------------------------------------
//| Generates a random uppercase letter
//+-----------------------------------------------------------------------------
CHAR RANDOM::UpperCaseLetter() CONST
{
	return static_cast<CHAR>(Int('A', 'Z'));
}
